<template>
  <div class="login">
    <div class="login-box">

      <el-form :model="loginForm" ref="loginForm" :rules="loginFormRule">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" clearable
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" clearable prefix-icon="el-icon-lock"
                    show-password></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="14">
            <el-form-item prop="kaptchaInfo">
              <el-input class="kaptchaInfo" v-model="loginForm.kaptchaInfo" placeholder="请输入验证码" clearable
                        prefix-icon="el-icon-message"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <div class="codeImg" @click="getKaptcha">
              <img :src="codeImg" alt="" class="codeImgBack">
            </div>
          </el-col>
        </el-row>


        <el-form-item>
          <el-button class="login-button" type="success" style="width: 100%;margin-top: 20px"
                     @click="login('loginForm')" @keyup.enter="keyDown(e)">登陆
          </el-button>
        </el-form-item>

      </el-form>


      <el-link type="primary" class="register" :underline="false" @click="register">注册用户</el-link>

      <el-link type="primary" class="forget-password" :underline="false" @click="forgetPassword">忘记密码?</el-link>
    </div>

    <Register :show="showRegister" @show="showRegisterMethod"></Register>
  </div>
</template>

<script>
import {kaptcha, loginByCode} from '@/api/login/index'
import Register from '@/views/register/index'
import {userInfo} from "@/api/register";
import websocket from "@/utils/websocket";

export default {
  components: {Register},
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        kaptchaInfo: '',
        kaptchaInfoKey: ''
      },
      codeImg: '',
      loginFormRule: {
        username: [
          {
            required: true, message: '请输入用户名', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          }
        ],
        kaptchaInfo: [
          {
            required: true, message: '请输入验证码', trigger: 'blur'
          }
        ]
      },
      showRegister: false
    }
  },
  methods: {
    //登录接口
    login(val) {
      this.$refs[val].validate((valid) => {
        if (valid) {
          loginByCode(this.loginForm).then(res => {
            //存储token
            if (res.code === 200) {
              sessionStorage.setItem("token", JSON.parse(res.data).access_token)
              this.queryUserDetails()
            }

          }).catch(error => {

          })
        }
      })
    },
    //注册用户
    register() {
      this.showRegister = true
    },
    //忘记密码
    forgetPassword() {
      this.$message.info("暂未开通服务")
    },
    //获取验证码
    getKaptcha() {
      kaptcha().then(res => {
        this.loginForm.kaptchaInfoKey = res.headers.key
        this.codeImg = window.URL.createObjectURL(res.data)
      }).catch(error => {

      })
    },
    //键盘按下事件
    keyDown(e) {
      if (e.keyCode === 13 || e.keyCode === 100) {
        this.login("loginForm")
      }
    },
    showRegisterMethod(val) {
      this.showRegister = val;
    },
    queryUserDetails() {
      userInfo().then(res => {
        sessionStorage.setItem("url", res.data.avatar)
        sessionStorage.setItem("nickname", res.data.nickname)
        sessionStorage.setItem("params", res.data.keySet)
        sessionStorage.setItem("menu", JSON.stringify(res.data.menu))
        //延迟执行
        this.$nextTick(()=>{
          websocket.init()
        })
        this.$router.push("/")
      }).catch(error => {

      })
    }
  },
  mounted() {
    //绑定键盘按下事件
    window.addEventListener("keydown", this.keyDown);
    this.getKaptcha();
  },
  destroyed() {
    //销毁键盘按下事件
    window.removeEventListener("keydown", this.keyDown, false)
  }
}
</script>

<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(#141e30, #243b55);
}

.login-box {
  width: 400px;
  height: 350px;
  border-radius: 10px 10px 10px 10px;
  background-color: rgba(0, 0, 0, 0.2);
  box-shadow: 0 15px 25px rgba(0, 0, 0, 0.4);
}

.login-box {
  ::v-deep .el-input {
    margin-top: 20px;
  }
}


.register {
  float: right
}

.forget-password {
  margin-right: 10px;
  float: right;
}

.login-button {
  background-color: rgba(0, 0, 0, 0.2);
}

.el-form {
  ::v-deep .el-form-item {
    padding-left: 20px;
    padding-right: 20px;
  }
}

.codeImg {
  margin-top: 22px;
  height: 40px;
}

.codeImgBack {
  border-radius: 5px;
  margin-left: 18px;
  background-size: 100%;
  height: 100%;
}
</style>